home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
pascal
/
oop55.zip
/
OOPDEMOS.DOC
< prev
next >
Wrap
Text File
|
1989-05-02
|
24KB
|
678 lines
Turbo Pascal 5.5 Documentation
Object-Oriented Programming Examples
This file documents the Turbo Pascal 5.5 Object-Oriented
Programming (OOP) examples. There are over 12,000 lines of
examples contained in the OOPDEMOS.ARC file on the disk labeled
OOP/DEMOS/BGI/DOC. (If you have a hard disk and run the INSTALL
program to install Turbo Pascal on your system, it will place the
OOP examples in C:\TP by default.)
TABLE OF CONTENTS
-----------------
1. OBJECTS.PAS - Basic object types unit
2. ODEMO.PAS - An example that uses streams and lists
3. FORMS.PAS - Implements a form entry and edit object
4. SLIDERS.PAS - Implements a slider field
5. FDEMO.PAS - A simple forms editor example
6. CARDS.PAS - Implements a card file object
7. CARDFILE.PAS - A simple card filer applciation
8. CARDGEN.PAS - Card filer forms generator
9. TCALC.PAS - See TCALC.DOC
10. Examples from the Object-Oriented Programming Guide
Four examples are included from the OOP Guide for your
convenience:
POINTS PAS - From P-20 of the OOP Guide
FIGURES PAS - From P-42 of the OOP Guide
FIGDEMO PAS - From P-47 of the OOP Guide
LISTDEMO PAS - From P-57 of the OOP Guide
These examples are fully documented in Chapter 1 of the OOP
Guide.
OBJECTS.PAS - BASIC OBJECT TYPES UNIT
-------------------------------------
The Objects unit implements two basic object types: a Stream and
a List. The Stream type is the object-oriented counterpart of a
Pascal file. Turbo Pascal 5.5 does not allow "file of object"
types, but streams may be used to implement the same
functionality, and much more as the example programs show. The
List type implements a singly-linked list of objects, each of
which must be derived from the Node type.
The Base type
-------------
Base is an abstract object type, and serves only as an ultimate
ancestor for other object types. Objects of type Base are never
instantiated. Object types derived from Base are guaranteed to
have a destructor called Done. In addition, the VMT field of
descendants of Base will always be the first field in the
descendant, which is a prerequisite of types registered with a
stream.
Unless overridden, the Done destructor in Base does nothing
except to dispose the instance when called via the extended
syntax of the Dispose standard procedure.
The Stream type
---------------
Much like an untyped file, a stream implements a number of basic
I/O capabilities, such as opening, closing, reading, writing, and
seeking. What sets a stream apart from an untyped file is its
ability to polymorphically read and write objects. This is best
demonstrated through an example.
Assume that you have three object types, Circle, Rectangle, and
Triangle, each of which are derived from an ancestor type Shape.
In order to read and write such objects to disk you would need a
FILE OF Shape that allows reading and writing of objects of type
Shape as well as descendants of Shape. For a number of reasons,
ordinary Pascal FILE types cannot achieve this. First, objects of
type Circle, Rectangle, and Triangle are most likely not of the
same size, since each will add a varying number of fields to the
basic Shape type. Thus, it would be impossible to determine the
proper record size for a FILE OF Shape. Second, a FILE OF Shape
would need to store additional type information for each object
in the file so that when reading the file, the application can
"know" the types of the objects it is reading.
The Stream type provides the solution to this problem: By
defining a Store and Load method in an object type, and by
registering that type with a stream, the stream can perform
polymorphic I/O through its Put and Get methods. The FORMS.PAS
unit and the CARDFILE.PAS program provide good examples on how to
use streams.
The Stream type is the ancestor of all other streams. It defines
the basic properties of a stream, but most of its methods are
purely abstract and meant to be overridden in descendant types.
FIELDS
TypeCount Number of types registered with the stream.
TypeList Pointer to array of VMT offsets of registered types.
ProcList Pointer to array of Store and Load method pointers.
Status Stream status. When Status is non-zero, an error
has occurred on the stream, and all subsequent
I/O is suspended. The Status field is the
equivalent of the IOResult standard function,
except that you have to manually clear Status to
re-enable I/O operations.
CONSTRUCTORS AND DESTRUCTORS
constructor Init;
Initializes the stream by allocating TypeList and ProcList, and
calling RegisterTypes to register the types known by the
stream.
destructor Done; virtual;
Disposes TypeList and ProcList.
BASIC I/O METHODS
procedure Read(var Buf; Count: Word); virtual;
Reads Count bytes from the stream into Buf. In case of error,
Buf is filled with zeros. This method must be overridden.
procedure Write(var Buf; Count: Word); virtual;
Writes Count bytes from Buf onto the stream. This method must
be overridden.
procedure Flush; virtual;
Flushes the stream's I/O buffer, if any. This method does
nothing unless overridden.
procedure Truncate; virtual;
Truncates the stream at the current position, i.e. makes the
current position the end of the stream. This method must be
overridden.
function GetPos: Longint; virtual;
Returns the current position of the stream, or -1 in case of
error.
procedure SetPos(Pos: Longint; Mode: Byte); virtual;
Sets the current position of the stream to a new position that
is Pos bytes from the stream location given by Mode. Valid Mode
values are given by the constants PosAbs, PosCur, and PosEnd,
which represent the stream's beginning, current position, and
end. This method must be overridden.
function GetSize: Longint;
Returns the current size of a stream. Calls SetPos and GetPos
to find the resulting value. This method should not be
overridden.
procedure Seek(Pos: Longint);
Seeks to the specified position. Corresponds to a call to
SetPos with a Mode value of PosAbs. This method should not be
overridden.
TYPE REGISTRATION METHODS
procedure RegisterTypes; virtual;
Registers all types that should be known to the stream. To
register types with a stream you must override this method and
call the Register method for each type. Within an overridden
RegisterTypes, always first call the inherited RegisterTypes to
register any types required by the ancestor. A type need only
be registered if instances of the type are read and written
using the Get and Put methods. Unless overridden, this method
does nothing.
procedure Register(TypePtr, StorePtr, LoadPtr: Pointer);
Registers a type with the stream. This method must only be used
within a RegisterTypes method. The format of a method call is:
Register(TypeOf(AType), @AType.Store, @AType.Load);
where AType is an object type derived from the Base object type
(i.e. an object type whose ultimate ancestor is Base). AType
must define Store and a methods with the following headers:
procedure Store(var S: Stream);
constructor Load(var S: Stream);
The Store method must write a binary representation of the
object onto the stream S (using S.Write), and the Load
constructor must read such a binary representation back from
the stream S (using S.Read).
POLYMORPHIC I/O METHODS
procedure Put(P: BasePtr);
Writes the specified object to the stream. The type of the
object must have been registered with the stream (using an
overridden RegisterTypes method). Put writes a 16-bit object
type identifier number onto the stream and then calls the
object's Store method, which writes a binary copy of the
object. The 16-bit object type identifier corresponds to the
index of the